<template>
  <div class="user-profile">
    <van-nav-bar
      title="个人信息"
      left-text="返回"
      left-arrow
      @click-left="$router.back()"
    />
    <input type="file" hidden ref="file" @change="fileChange" />
    <van-cell title="头像" is-link @click="$refs.file.click()">
      <van-image width="30" height="30" fit="cover" :src="user.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      is-link
      :value="user.name"
      @click="nameshow = true"
    />
    <van-cell title="性别" is-link :value="user.gender === 0 ? '男' : '女'" />
    <van-cell title="生日" is-link :value="user.birthday" />
    <van-popup
      v-model="nameshow"
      position="top"
      style="height: 100%; width: 100%"
      ><updateName
        @close="nameshow = false"
        v-model="user.name"
        v-if="nameshow"
    /></van-popup>
    <!-- 头像弹出层 -->
    <van-popup
      v-model="touxiangshow"
      position="top"
      :style="{ height: '100%' }"
    >
      <updateTouxiang
        :img="img"
        @close="touxiangshow = false"
        v-if="touxiangshow"
        @update-photo="user.photo = $event"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '../../api/user'
import updateName from './component/update-name.vue'
import updateTouxiang from './component/update-photo.vue'
export default {
  name: 'userIndex',

  data() {
    return {
      user: {},
      nameshow: false,
      touxiangshow: false,
      img: null

    };
  },
  components: {
    updateName,
    updateTouxiang
  },
  created() {
    this.loadUserProfile()
  },
  mounted() {

  },

  methods: {
    async loadUserProfile() {
      try {
        const { data } = await getUserProfile()
        this.user = data.data
        console.log(data.data);
      } catch (error) {
        this.$toast('获取信息失败！')
      }
    },
    fileChange() {
      // 读取文件对象
      const file = this.$refs.file.files[0]
      //  基于文章对象获取blob数据
      const data = window.URL.createObjectURL(file)
      this.img = data
      this.touxiangshow = true
      //file-input如果选了同一个文件不会触发change事件
      //解决办法就是每次使用完毕，把它的value清空
      this.$refs.file.value = ''
    }
  },
};
</script>

<style lang="less" scoped>
</style>